package com.ftwj.demo.base.util;

import java.net.URI;
import java.net.URL;
import java.util.Date;
import java.util.Locale;

import org.springframework.util.ClassUtils;
import cn.hutool.json.JSONUtil;
/**
*
* <p>Title: CacheKeyGenerator</p>  
* <p>Description: 描述:缓存主键生成器</p>
* <p>Copyright: Copyright (c) 2020 </p>  
* <p>Company: 兰州飞天网景信息产业有限公司</p>    
* @author WangSLi
* @date 2020年5月6日 上午8:41:08
* @version V1.0
 */
class CacheKeyGenerator {

	private final static int NO_PARAM_KEY = 0;

	public static String generate(Object... params) {
		StringBuilder key = new StringBuilder(64);
		if (params.length > 0) {
			for (int i=0;i<params.length;i++) {
				if(i>0) key.append(",");
				if (isSimpleValueType(params[i].getClass())) {
					key.append(params[i]);
				} else {
					key.append(JSONUtil.toJsonStr(params[i]).hashCode());
					// Hashing.murmur3_128().hashString(key,Charset.defaultCharset()).asLong();
				}
			}
		} else {
			key.append(NO_PARAM_KEY);
		}
		return key.toString();
	}

	public static boolean isSimpleValueType(Class<?> cls) {
		return ClassUtils.isPrimitiveOrWrapper(cls) || cls.isEnum() || CharSequence.class.isAssignableFrom(cls)
			|| Number.class.isAssignableFrom(cls) || Date.class.isAssignableFrom(cls) 
			|| URI.class == cls || URL.class == cls || Locale.class == cls || Class.class == cls;
	}

}
